
var editRole = null;


$(function () {
});


var closeEdit = function () {
    $('#dl_role_edit').dialog("close");
}


var addRole = function () {

    $('#form_role_edit').form('clear');
    $('#form_role_edit select[name="role.type"]').val(1);
    $('#form_role_edit select[name="role.state"]').val(1);

    editRole = ctx + "/sys/role_add";

    $('#form_role_edit input[name="role.id"]').prop("readonly", false);

    $('#dl_role_edit').dialog("open");
}


var updateRole = function () {
    $('#form_role_edit').form('clear');
    editRole = ctx + "/sys/role_update";

    var selected = $('#dg_role').datagrid("getSelected");
    if (!selected) {
        $.messager.alert('系统提示', "请选择你要修改的菜单");
        return;
    }
    //往表单放入原来的值
    $('#form_role_edit input[name="role.id"]').val(selected.id);
    $('#form_role_edit input[name="role.name"]').val(selected.name);
    $('#form_role_edit select[name="role.type"]').val(selected.type);
    $('#form_role_edit select[name="role.state"]').val(selected.state);


    $('#form_role_edit input[name="role.id"]').prop("readonly", "readonly");
    $('#dl_role_edit').dialog("open");
}


var submitRole = function () {
    $('#form_role_edit').form('submit', {
        url: editRole,

        success: function (data) {

            data = eval('(' + data + ')');
            if (data && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
            closeEdit();

            $('#dg_role').datagrid("reload");
        }
    });
}


var removeRole = function () {
    var selected = $('#dg_role').datagrid("getSelected");
    if (!selected) {
        $.messager.alert('系统提示', "请选择你要删除的菜单");
        return;
    }
    var id = selected.id;

    $.messager.confirm('系统提示', '您确定要删除菜单“' + selected.name + '”吗?', function (r) {
        if (r) {
            $.post(ctx + "/sys/role_delete", {'role.id': id}, function (data) {
                if (data && data.msg) {
                    $.messager.alert('系统提示', data.msg);
                }
                $('#dg_role').datagrid("reload");
            });
        }
    });

}


//查询
var searchRole = function () {

    $('#dg_role').datagrid('load', {
        'role.id': $('#searRoleParams input[name="role.id"]').val(),
        'role.name': $('#searRoleParams input[name="role.name"]').val(),
        'role.type':$('#searRoleParams input[name="role.type"]').val()
    });
}
